<?php

//echo $_POST["who"];

$select = "select * from zakazka where cislo_objednavky = '".urldecode($_GET["updatez"])."'";
if (Is_Array($pole_z = DbSelect($select)))
{
  $z_id = $pole_z["id"];
}

  // upravy
$d = strtok($_POST["datum_zadani"], '/');
$m = strtok('/');
$r = strtok('/');
$datum_zadani = "$r-$m-$d 00:00:00";
$_POST["datum_zadani"] = $datum_zadani;

$d = strtok($_POST["termin_odevzdani"], '/');
$m = strtok('/');
$r = strtok('/');
//$h = strtok('/');
$termin_odevzdani = "$r-$m-$d 00:00:00";
$_POST["termin_odevzdani"] = $termin_odevzdani;

$_POST["naklady"] = str_replace(",", ".", $_POST["naklady"]);
$_POST["cena_celkem"] = str_replace(",", ".", $_POST["cena_celkem"]);

$uz_je = false;
$update = "update zakazka set ";

function helper($pole, $co)
{
  if ($pole[$co] != $_POST[$co])
  {
    if ($GLOBALS["uz_je"])
    {
      $GLOBALS["update"].=" , ";
    }
    $GLOBALS["uz_je"]=true;
    $GLOBALS["update"].= " $co = '".$_POST[$co]."' ";

    if (isset($_GET["addz"]) || isset($_GET["addpz"]))
    {
      //echo $co."<br>";
    }
    else
    {
      $id = vrat_max_id("dbupdate") + 1;
      $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".urldecode($_GET["updatez"])."', '$co', '".$pole[$co]."', '".$_POST[$co]."', 0, '', CURRENT_TIMESTAMP())";
      DbQuery($insert, $_POST["who"]);
    }
  }
}

helper($pole_z, "klient_id");
helper($pole_z, "zakazku_zadal");
helper($pole_z, "doprava_athis");
helper($pole_z, "zpusob_dodani");
helper($pole_z, "datum_zadani");
helper($pole_z, "termin_odevzdani");
helper($pole_z, "projekt");
helper($pole_z, "popis_zakazky");
helper($pole_z, "cena_celkem");
helper($pole_z, "naklady");
helper($pole_z, "poznamka_obchodni");
helper($pole_z, "spokojenost_klienta");


$update .= " where cislo_objednavky = '".urldecode($_GET["updatez"])."'";
//echo $update."<br>";

DbQuery($update, $_POST["who"]);

// prev hodnota pro dbupdate

// delete typ_zakazky

// insert typ_zakazky

// next hodnota pro dbupdate

$i = 1;
// najit format_id
$select = " select "
          ."   group_concat(tz.typ separator ', ')  as typ, "
          ."   group_concat(tz.id separator '')  as ids, "
          ."   v.zakazka_id as id "
          ." from "
          ."   vztah_zakazka_typ as v, "
          ."   typ_zakazky as tz "
          ." where "
          ."   (v.zakazka_id = ".$pole_z["id"].") "
          ."   and (v.typ_id = tz.id) "
          ." group by "
          ."   v.zakazka_id ";

//echo $select."<br>";
$prev = DbSelect($select);

$res_n = MySQL_Query("select count(id) as n from typ_zakazky", $spojeni);
$pole_n = MySQL_Fetch_Array($res_n);
$n = $pole_n["n"];
//
// snazi se o ulozeni M:N vztahu zakazka:typ_zakazky
//
$ids = "";
$i = 0;
while ($i < $n)
{
  if ($_POST["typ_zakazky_$i"] != "")
  {
    $ids .= $_POST["typ_zakazky_$i"];
  }
  $i++;
}
if ($ids != $prev["ids"])
{
  $delete = "delete from vztah_zakazka_typ where zakazka_id = ".$pole_z["id"];
  DbQuery($delete, $_POST["who"]);
  $i = 0;
  while ($i < $n)
  {
    if ($_POST["typ_zakazky_$i"] != "")
    {
      $insert = "insert into vztah_zakazka_typ values (".$pole_z["id"].", ".$_POST["typ_zakazky_$i"].")";
      //echo "".$insert."<br>";
      DbQuery($insert, $_POST["who"]);
    }
    $i++;
  }
  $next = DbSelect($select);

  if (isset($_GET["addz"]) || isset($_GET["addpz"]))
  {
    //echo $co."<br>";
  }
  else
  {
    $id = vrat_max_id("dbupdate") + 1;
    $insert = "insert into dbupdate values ($id, '".$_POST["who"]."', '".$_GET["updatez"]."', 'typ_zakazky', '".$prev["typ"]."', '".$next["typ"]."', '0', '', CURRENT_TIMESTAMP())";
    DbQuery($insert, $_POST["who"]);
  }
}
else
{
  // typ zakazky se nezmenil, vybery jsou stejne
}

?>


